package com.bie.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.woke.bean.UserBean;
import com.woke.dao.Customer;
import com.woke.dao.User;

public class LoginAction implements Action, ServletRequestAware, ServletResponseAware, ServletContextAware {
	private HttpServletRequest request;
	private HttpServletResponse responese;
	private HttpSession session;
	private ServletContext application;
	private String username;
	private String password;
	private User u;
	private String id;
	private String msg;
	private String role;
	private String tel;
	
	
	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public User getU() {
		return u;
	}

	public void setU(User u) {
		this.u = u;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	
	public String execute() throws Exception {

		return null;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	public void setServletResponse(HttpServletResponse response) {
		this.responese = response;

	}

	public void setServletContext(ServletContext application) {
		this.application = application;

	}
	
	

	public String login() throws Exception {
		role= new String(role.getBytes("ISO-8859-1"),"utf-8");
		username= new String(username.getBytes("ISO-8859-1"),"utf-8");
		password= new String(password.getBytes("ISO-8859-1"),"utf-8");
		String sql = "select * from user where username='" + username + "' and password = '" + password + "' and role='"+role+"'";
		List<User> list = User.getData(sql);
		if (list != null && list.size() > 0) {
			ActionContext actionContext = ActionContext.getContext();
			Map session = actionContext.getSession(); 
			session.put("username", username);
			session.put("role", role);
			ShuJuAction sj=new ShuJuAction();
			sj.getnum1();
			sj.getMonthNum();
			sj.getMonthNum1();
			sj.getMonthNum2();
			request.getSession().setAttribute("user", list.get(0));
			return "index";
		} else {
			return "login";
		}
	}
	
	
	public String checktel() throws Exception {
		String sql = "select * from user where tel='" + tel + "'";
		String sql1="select * from user";
		List<User> list1=User.getData(sql1);
		List<User> list = User.getData(sql);
		if (list != null && list.size() > 0) {
			int num=list1.size();
			ActionContext actionContext = ActionContext.getContext();
			Map session = actionContext.getSession(); 
			request.getSession().setAttribute("num", num);
			session.put("username", username);
			session.put("role", role);
			request.getSession().setAttribute("user", list.get(0));
			return "index";
		} else {
			msg="1";
			return "main";
		}
	}

	public String sigout() throws Exception {
		ActionContext.getContext().getSession().clear();
		return "signin";
	}
	
	public String getindex() throws Exception {
		String sql1="select * from user";
		List<User> list1=User.getData(sql1);
			int num=list1.size();
			ActionContext actionContext = ActionContext.getContext();
			Map session = actionContext.getSession(); 
			request.getSession().setAttribute("num", num);
			return "index1";
		} 
	
}
